﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using starLiGHT;
using starLiGHT.Physic.TwoD;
using starLiGHT.Render;

#endregion

#region COPYRIGHT

/*
    Copyright (c) 2008, 2009, 2010 
        Roland Rosenkranz (Glatzemann@email.de)
*/

#endregion

#region LICENSE

/*
    This file is part of starLiGHT.Collision.

    starLiGHT.Collision is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Collision is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Collision.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
*/

#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1566                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-10-20 14:55:54 #$:  Date of last commit     *
// ************************************************************
#endregion

namespace starLiGHT.Physic.TwoD
{
    public class DebugRenderZune : DebugDraw
    {
        AABB worldAABB;
        float screenWidth;
        float screenHeight;
        public Vector2 screenOffset = Vector2.Zero;
        TextureSurface debugSurface;
        SpriteBatch spriteBatch;
        ICamera2D camera;
        bool flipY;

        public Matrix ViewMatrix
        {
            get { return Matrix.Identity; }
            set { }
        }

        public Matrix ProjectionMatrix
        {
            get { return Matrix.Identity; }
            set {  }
        }

        public Matrix WorldMatrix
        {
            get { return Matrix.Identity; }
            set {  }
        }

        public override void updateCamera(ICamera2D camera, bool flipY)
        {
            this.flipY = flipY;

            //primitiveBatch.ProjectionMatrix = Matrix.CreateOrthographic(camera.Size.X / camera.Zoom, (-camera.Size.Y * (flipY ? -1f : 1f)) / camera.Zoom, 0.0f, 1.0f);
            //primitiveBatch.WorldMatrix = Matrix.CreateTranslation(-camera.Size.X * 0.5f, -camera.Size.Y * 0.5f, 0.0f);
            //primitiveBatch.ViewMatrix = camera.CameraMatrixNoScale;
        }

        Vector2 transform(Vector2 src)
        {
            src = Vector2.Transform(src, camera.GetCameraMatrix(0)) * camera.Zoom;
            src += screenOffset;
            return flip(src);
        }

        Vector2 flip(Vector2 src)
        {
            if (flipY)
                src.Y = screenHeight - src.Y;

            return src;
        }

        public DebugRenderZune(GraphicsDevice graphicsDevice, ICamera2D camera, AABB worldAABB, float screenWidth, float screenHeight, float scale)
        {
            this.camera = camera;

            spriteBatch = new SpriteBatch(graphicsDevice);
            debugSurface = TextureGenerator.CreateSurface(graphicsDevice, (int)screenWidth, (int)screenHeight, Color.Black);

            this.worldAABB = worldAABB;
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;

            float worldWidth = worldAABB.upperBound.X - worldAABB.lowerBound.X;
            float worldHeight = worldAABB.upperBound.Y - worldAABB.lowerBound.Y;
        }

        ~DebugRenderZune()
        {
            if (debugSurface != null)
            {
                TextureGenerator.RemoveSurface(debugSurface);
                debugSurface = null;
            }
        }

        public override void BeginFrame()
        {
            base.BeginFrame();
            TextureGenerator.StartSurface(debugSurface, true);
        }

        public override void EndFrame()
        {
            base.EndFrame();
            Texture2D tex = TextureGenerator.EndSurface(debugSurface);
            spriteBatch.Begin();
            spriteBatch.Draw(tex, Vector2.Zero, Color.White);
            spriteBatch.End();
        }

        public override void DrawPolygon(Vector2[] vertices, int vertexCount, Color color)
        {
            for (int i = 1; i < vertexCount; i++)
            {
                TextureGenerator.DrawLine(debugSurface, 
                                          transform(vertices[i - 1]),
                                          transform(vertices[i]),
                                          color);
            }

            TextureGenerator.DrawLine(debugSurface,
                                      transform(vertices[vertexCount - 1]),
                                      transform(vertices[0]),
                                      color);
        }

        public override void DrawSolidPolygon(Vector2[] vertices, int vertexCount, Color color)
        {
            if (vertexCount > 2)
            {
                //primitiveBatch.Begin(PrimitiveType.TriangleFan);

                Color fillColor = new Color((byte)(color.R / 2), (byte)(color.G / 2), (byte)(color.B / 2), (byte)128);

                for (int i = 0; i < vertexCount; i++)
                {
                    //primitiveBatch.AddVertex(screenOffset + vertices[i], fillColor);
                }

                //primitiveBatch.End();
            }

            Vector2 v1, v2;
            for (int i = 1; i < vertexCount; i++)
            {
                v1 = transform(vertices[i - 1]);
                v2 = transform(vertices[i]);

                TextureGenerator.DrawLine(debugSurface, v1, v2, color);
            }
            v1 = transform(vertices[vertexCount - 1]);
            v2 = transform(vertices[0]);
            TextureGenerator.DrawLine(debugSurface, v1, v2, color);
        }

        public override void DrawCircle(Vector2 center, float radius, Color color)
        {
            float k_segments = 16.0f;
            float k_increment = 2.0f * PhysicSettings.b2_pi / k_segments;
            float theta = 0.0f;
            Vector2 v = Vector2.Zero;
            Vector2 first = Vector2.Zero;
            Vector2 last = Vector2.Zero;

            for (int i = 0; i < k_segments; ++i)
            {
                if (i == 0)
                {
                    first = last = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                }
                else
                {
                    if (i > 1) last = v;
                    v = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                    TextureGenerator.DrawLine(debugSurface, transform(last), transform(v), color);
                }

                theta += k_increment;
            }

            TextureGenerator.DrawLine(debugSurface, transform(v), transform(first), color);
        }

        public override void DrawSolidCircle(Vector2 center, float radius, Vector2 axis, Color color)
        {
            float k_segments = 16.0f;
            float k_increment = 2.0f * PhysicSettings.b2_pi / k_segments;
            float theta = 0.0f; 
            
            Color fillColor = new Color((byte)(color.R / 2), (byte)(color.G / 2), (byte)(color.B / 2), (byte)128);

            Vector2 v = Vector2.Zero;

            //primitiveBatch.Begin(PrimitiveType.TriangleFan);

            for (int i = 0; i < k_segments; ++i)
            {
                v = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                //primitiveBatch.AddVertex(screenOffset + v, fillColor);
                theta += k_increment;
            }

            //primitiveBatch.End();

            //primitiveBatch.Begin(PrimitiveType.LineList);
            theta = 0.0f;
            Vector2 last = Vector2.Zero;
            Vector2 first = Vector2.Zero;

            for (int i = 0; i < k_segments; ++i)
            {
                if (i == 0)
                {
                    first = last = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                }
                else
                {
                    if (i > 1) last = v;
                    v = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                    TextureGenerator.DrawLine(debugSurface, transform(last), transform(v), color);
                }

                theta += k_increment;
            }

            TextureGenerator.DrawLine(debugSurface, transform(v), transform(first), color);

            Vector2 p = center + radius * axis;
            TextureGenerator.DrawLine(debugSurface, transform(center), transform(p), color);
        }

        public override void DrawSegment(Vector2 p1, Vector2 p2, Color color)
        {
            TextureGenerator.DrawLine(debugSurface, transform(p2), transform(p1), color);
        }

        public override void DrawTransform(Transform xf)
        {
            throw new NotImplementedException();
        }

        public override void DrawPoint(Vector2 p, float size, Color color)
        {
            //primitiveBatch.Begin(PrimitiveType.PointList);
            //primitiveBatch.PointSize = size;

            //primitiveBatch.AddVertex(screenOffset + p, color);

            //primitiveBatch.End();
        }

        public override void DrawString(SpriteBatch spriteBatch, SpriteFont font, int x, int y, string text)
        {
            if (!String.IsNullOrEmpty(text))
                spriteBatch.DrawString(font, text, new Vector2(x, y), Color.White);
        }

        public override void DrawAABB(ref AABB aabb, Color color)
        {
            //primitiveBatch.Begin(PrimitiveType.LineList);

            //primitiveBatch.AddVertex(new Vector2(aabb.lowerBound.X, aabb.lowerBound.Y), color);
            //primitiveBatch.AddVertex(new Vector2(aabb.upperBound.X, aabb.lowerBound.Y), color);

            //primitiveBatch.AddVertex(new Vector2(aabb.upperBound.X, aabb.lowerBound.Y), color);
            //primitiveBatch.AddVertex(new Vector2(aabb.upperBound.X, aabb.upperBound.Y), color);

            //primitiveBatch.AddVertex(new Vector2(aabb.upperBound.X, aabb.upperBound.Y), color);
            //primitiveBatch.AddVertex(new Vector2(aabb.lowerBound.X, aabb.upperBound.Y), color);

            //primitiveBatch.AddVertex(new Vector2(aabb.lowerBound.X, aabb.upperBound.Y), color);
            //primitiveBatch.AddVertex(new Vector2(aabb.lowerBound.X, aabb.lowerBound.Y), color);

            //primitiveBatch.End();
        }
    }
}
